package fic.storefinder.client.content;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
import com.google.gwt.user.client.ui.VerticalPanel;

import fic.storefinder.shared.serializable.Robe;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;

public class ImageUploaderDialog extends DialogBox {

	public ImageUploaderDialog(Robe robe) {
		
		final FormPanel form = new FormPanel();
		form.setAction(GWT.getModuleBaseURL() + "uploadimage");

		
		form.setEncoding(FormPanel.ENCODING_MULTIPART);
		form.setMethod(FormPanel.METHOD_POST);

	
		VerticalPanel panel = new VerticalPanel();
		form.setWidget(panel);
		
		Label lblKpFeltltse = new Label("Kép feltöltése");
		lblKpFeltltse.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		panel.add(lblKpFeltltse);

		
		final FileUpload upload = new FileUpload();
		upload.setName("" + robe.getRobeId());
		panel.add(upload);

	
		panel.add(new Button("Feltöltés", new ClickHandler() {
			public void onClick(ClickEvent event) {
				form.submit();
			}
		}));

	
		form.addSubmitHandler(new FormPanel.SubmitHandler() {
			public void onSubmit(SubmitEvent event) {
				if (upload.getFilename().length() == 0) {
					Window.alert("Válasszon ki egy képet feltöltésre!");
					event.cancel();
				}
			}
		});

		form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
			public void onSubmitComplete(SubmitCompleteEvent event) {
				ImageUploaderDialog.this.hide();
			}
		});

		setWidget(form);
	}

}
